﻿using BridgeShips.Control;
using UnityEngine;
using UnityEngine.AI;

namespace BridgeShips.AI {
    [System.Serializable]
    public class AIEntityMovement {
        public Vector3 CurrentDestination { get { return m_CurrentDestination; } }

        public ET.AIMovementState MovementState { get { return m_MovementState; } set { m_MovementState = value; } }

        //public NavMeshAgent Agent { get { return m_Agent; } }

        [SerializeField]
        [Tooltip( "Normal speed the agent will use." )]
        private float m_DriveSpeed;

        [SerializeField]
        [Tooltip( "Distance-vale need to be stopped." )]
        private float m_StoppingDistance;

        private float m_RemainDistance = 1000;
        private float m_RemainRotation = 0.0f;
        private Vector3 m_CurrentDestination;
        private Vector3 m_CurrentForward;
        private ET.AIMovementState m_MovementState;

        private AIShip m_Ship;
        //private NavMeshAgent m_Agent;


        public void Initialize( AIShip ship ) {
            m_Ship = ship;

            //m_Agent = m_Ship.GetComponent<NavMeshAgent>();
        }

        public void Update( Transform transform ) {
            if (m_CurrentDestination.Equals( Vector3.zero ))
                return;
            if (m_Ship != null && m_Ship.Settings.Detection.HaveLinked() == false
                && m_MovementState == ET.AIMovementState.AIDriving) {
                var shipPosition = m_Ship.Settings.Detection.EntityTransform().position;
                shipPosition.y = 7.0f;
    
                GameEntry.Instance.EntityManager.LerpForwardLocalRotation(transform, m_CurrentForward);
                m_RemainDistance = GameEntry.Instance.EntityManager.LerpPosition(transform, m_CurrentDestination);
                
                m_RemainRotation = Mathf.Floor( Vector3.Angle( transform.forward, m_CurrentForward ));

                m_CurrentForward = transform.forward;
            }
        }

        private Vector3 RoundValue( Vector3 vecValue) {
            vecValue.x = ( float)Mathf.Round( vecValue.x * 10000 ) / 10000;
            vecValue.z = (float)Mathf.Round( vecValue.z * 10000 ) / 10000;
            return vecValue;
        }
        /// <summary>
        /// Function called from different actions that allows the movement of the AI from one point to another.
        /// </summary>
        /// <param name="position"></param>
		public NavMeshPath MoveTo( Vector3 position, bool fastMove = false ) {
            var path = new NavMeshPath();

            m_CurrentDestination = position;
            m_MovementState = ET.AIMovementState.AIDriving;

            return path;
        }

        public void MoveTo( Vector3 position, Vector3 forward ) {
            //We assign the current target.
            m_CurrentDestination = position;
            m_CurrentForward = forward;
            m_MovementState = ET.AIMovementState.AIDriving;
        }

        private void ChangeMovementState( float speed, string animName, bool animValue, ET.AIMovementState newState ) {
            m_MovementState = newState;
        }

        /// <summary>
        /// Help function called from some actions that allows to check if the AI has already arrived it's current target point(m_CurrentDestination).
        /// </summary>
        /// <returns></returns>
        public bool ReachedDestination( bool isStop = true ) {
            if (m_RemainDistance <= m_StoppingDistance && m_RemainRotation < 0.05f) {
                if (isStop && m_Ship.Settings.Movement.MovementState == ET.AIMovementState.AIDriving) {
                    m_MovementState = ET.AIMovementState.WaitingIdle;
                    Debug.LogError("ReachedDestination here!!");

                }
                return true;
            }
            return false;
        }

        public void MakeLinked(bool link) {
            var transform = m_Ship.Settings.Detection.EntityTransform();
            var shipHander = transform.GetComponent<AIEventHandler>(); 
            if (shipHander != null) {
                shipHander.IsLinked = new Value_T<bool>( link );
                GameObject obj = GameObject.Find("Ship List");
                ShowShipsList tempList = obj.GetComponent<ShowShipsList>();
                uint id = uint.Parse(transform.name.Substring(transform.name.Length - 1));
                tempList.ChangeShipListState(id,link);
            }
        }
    }
}